package team.freeroom.hotelmanage.config;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;
import team.freeroom.hotelmanage.controller.interceptor.LoginInterceptor;

@Configurable
@ComponentScan(value = {"team.freeroom.hotelmanage.controller"})
// 启用 SpringMVC <mvc:annotation-driven/>
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    /**
     * 设置由 web容器处理静态资源 ，相当于 xml中的 <mvc:default-servlet-handler/>
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /**
     * 视图解析器
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        // 默认前缀 /WEB-INF/   后缀.jsp
        registry.jsp("/WEB-INF/view/", ".jsp");
    }

    /**
     * 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 创建自定义的拦截器
        HandlerInterceptor interceptor = new LoginInterceptor();
        // 添加拦截器
        registry.addInterceptor(interceptor)
                // 添加需要拦截的路径
                .addPathPatterns("/**")
                // 不需要拦截的路径
                .excludePathPatterns("/system/login", "/system/get_cpacha", "/resource/**");
    }

    /**
     * 文件上传配置
     */
    @Bean
    public MultipartResolver multipartResolver(){
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(100*1024*1024);
        multipartResolver.setDefaultEncoding("UTF-8");
        return multipartResolver;
    }
}
